/*
 * Created on 2005-1-24
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.nauu.atombuilder.modle;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

/**
 * @author nauu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TextField extends Node implements IPropertySource {
	
	private static final long serialVersionUID = 1L;

	public static final Dimension SIZE = new Dimension(195, 30);
	
	final public static String PROP_LOCATION = "LOCATION";

	final public static String PROP_ID = "ID";
	
	final public static String PROP_PATH = "PROP_PATH";

    final public static String PROP_NAME = "NAME";

    final public static String PROP_VISIBLE = "VISIBLE";

    final public static String PROP_INPUTS = "INPUTS";

    final public static String PROP_OUTPUTS = "OUTPUTS";

    protected Point location = new Point(0, 0);

    protected String name = "TextField";

    protected String id;

    protected String path;

    protected static IPropertyDescriptor[] descriptors;

    public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
        return name;
    }

    public void setName(String name) {
        if (this.name.equals(name)) {
            return;
        }
        this.name = name;
        firePropertyChange(PROP_NAME, null, name);
    }

    @Override
    public void setSize(Dimension size) {
    	super.setSize(SIZE);
    }
    
    @Override
    public Dimension getSize() {
    	return SIZE;
    }

    public void setLocation(Point p) {
        if (this.location.equals(p)) {
            return;
        }
        this.location = p;
        firePropertyChange(PROP_LOCATION, null, p);
    }

    public Point getLocation() {
        return location;
    }

    //------------------------------------------------------------------------
    // Abstract methods from IPropertySource

    public Object getEditableValue() {
        return this;
    }

    public IPropertyDescriptor[] getPropertyDescriptors() {
    	if(descriptors == null){
    		
    		ComboBoxPropertyDescriptor visible =  new ComboBoxPropertyDescriptor(PROP_VISIBLE, "visible", new String[] { "true", "false" });
        	visible.setCategory("基本属性");
    		
        	ComboBoxPropertyDescriptor mode = new ComboBoxPropertyDescriptor(PROP_MODE, "mode", new String[] { "loop", "pop" });
        	mode.setCategory("基本属性");
    		
    		PropertyDescriptor id = new PropertyDescriptor(PROP_ID, "id");
	    	id.setCategory("基本属性");
	    	
	    	TextPropertyDescriptor name = new TextPropertyDescriptor(PROP_NAME, "name");
	    	name.setCategory("基本属性");
	    	
	    	TextPropertyDescriptor classpath = new TextPropertyDescriptor(PROP_PATH, "path");
	    	classpath.setCategory("基本属性");
	    	descriptors = new IPropertyDescriptor[]{
    			visible,
    			mode,
	        	id,
	        	name,
	        	classpath
	        };
    	}else{
    		if(descriptors.length == 0)
    			descriptors = new IPropertyDescriptor[]{};
    	}
    	return descriptors;
    }

    public Object getPropertyValue(Object id) {
    	if (PROP_VISIBLE.equals(id))
            return isVisible() ? new Integer(0) : new Integer(1);
    	 if (PROP_ID.equals(id))
             return getId();
        if (PROP_NAME.equals(id))
            return getName();
        if (PROP_MODE.equals(id)){
        	return getMode() ? new Integer(0) : new Integer(1);
        }
        if (PROP_PATH.equals(id))
            return getPath();
        return null;
    }

    public boolean isPropertySet(Object id) {
        return true;
    }

    public void resetPropertyValue(Object id) {

    }

    public void setPropertyValue(Object id, Object value) {
    	if (PROP_VISIBLE.equals(id))
            setVisible(((Integer) value).intValue() == 0);
        if (PROP_ID.equals(id))
            setId((String) value);
        if (PROP_NAME.equals(id))
        	setName((String) value);
        if (PROP_MODE.equals(id))
        	setMode(((Integer) value).intValue() == 0);
        if (PROP_PATH.equals(id))
            setPath((String)value);
    }
}